﻿using LibNiconico.NiconicoApi.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LibNiconico.NiconicoApi.User.Thumb
{
    /// <summary>
    /// ニコニコ動画ユーザthumb取得レスポンス
    /// </summary>
    public sealed class NicoVideoGetThumbUserResponse 
    {
        private NicoVideoGetThumbUserResponse(XDocument document)
        {
            UserImagePath = "";
            UserName = "";

            try
            {

                XElement body = document.Element("html").Element("body");

                var userNameElemnents = from element2 in
                                            (from element in body.Descendants("div")
                                             where (string)element.Attribute("class") == "user_img" && element.Element("a") != null
                                             select element.Element("a"))
                                        where element2 != null && element2.Element("img") != null
                                        select element2.Element("img").Attribute("src").Value;


                if (userNameElemnents.Count() > 0)
                {
                    UserImagePath = userNameElemnents.First();
                }

                var userNameElements = from element in body.Descendants("p")
                                       where element.Attribute("class") != null && element.Element("a") != null && element.Element("a").FirstNode != null
                                       select element.Element("a").Elements().First().Value;

                if (userNameElements.Count() > 0)
                {
                    UserName = userNameElements.First();
                }

            }
            catch (Exception e)
            {

            }
            
            

        }

        internal static NicoVideoGetThumbUserResponse Parse(XDocument document)
        {
            return new NicoVideoGetThumbUserResponse(document);
        }

        
        /// <summary>
        /// ユーザ名
        /// </summary>
        public string UserName { get; private set; }

        /// <summary>
        /// ユーザサムネイルパス
        /// </summary>
        public string UserImagePath { get; private set; }
    }
}
